<script setup>
	import {reactive, ref} from "vue";
	import {ElMessage, ElMessageBox} from "element-plus";
	import {Delete, Plus, Download, Upload, Message, Edit, Search,Promotion} from "@element-plus/icons-vue";
	import orderApi from "@/api/order/order.js";
	
	const list = ref([])
	const total = ref(0)
	
	//分页信息和搜索条件
	const orderQuery = reactive({
		userName: '',
		page: 1,
		limit: 10
	})
	
	//加载表格数据
	const loadData = () => {
		orderApi.list(orderQuery).then(result => {
			list.value = result.data.list
			total.value = result.data.total
		})
	}
	loadData()
	
	//搜索
	const onSearch = () => {
		orderQuery.page = 1
		loadData()
	}
	//重置按钮
	const originData = () => ({
		userName: "",
		beginCreateTime: "",
		endCreateTime: ""
	});
	const resetForm = () => {
		Object.assign(orderQuery, originData());
		loadData();
	};
	
	//单个删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				orderApi.deleteById(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//批量删除
	const ids = ref([])
	const handleSelectionChange = (rows) => {
		ids.value = rows.map(row => row.id)
	}
	const deleteAll = () => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				ids.value = ids.value.join(',')
				orderApi.deleteAll(ids.value).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//查看订单详情
	const dialogFormVisible = ref(false)
	const orderInfo=ref({})
	const orderItemList = ref([])
	const title = ref()
	const details = (id) => {
		orderItemList.value = []
		orderInfo.value={}
		orderApi.selectById(id).then(res=>{
			if(res.code==0){
				orderInfo.value=res.data
				dialogFormVisible.value=true
				title.value='订单：'+orderInfo.value.orderNo+'详情'
				orderItemList.value=orderInfo.value.orderItemList
			}
		})
	}
	
	//发货
	const deliver = (id) => {
		orderApi.deliver(id).then(result=>{
			if (result.code === 0) {
				ElMessage.success(result.msg)
				loadData();
			} else {
				ElMessage.error(result.msg)
			}
		})
	}
	
	//导出Excel
	const exportExcel = () => {
		orderApi.exportExcel().then(result => {
			let blob = new Blob([result], {type: "application/xlsx"});
			let url = window.URL.createObjectURL(blob);
			const link = document.createElement("a"); // 创建a标签
			link.href = url;
			link.download = "订单列表.xlsx"; // 重命名文件
			link.click();
			URL.revokeObjectURL(url);
		})
	}
</script>

<template>
	<el-card class="">
		<template #header>
			<div class="header">
				<el-button type="danger" :icon="Delete" @click="deleteAll">批量删除</el-button>
				<el-button type="primary" :icon="Download" @click="exportExcel">导出Excel</el-button>
			</div>
		</template>
		<el-form :inline="true">
			<el-form-item label="用户名">
				<el-input v-model="orderQuery.userName" placeholder="请输入用户名" clearable/>
			</el-form-item>
			<el-form-item label="下单时间">
				<el-date-picker
					v-model="orderQuery.beginCreateTime"
					type="datetime"
					value-format="YYYY-MM-DD HH:mm:ss"
					placeholder="开始时间"
					style="width: 180px;"
				/>
				-
				<el-date-picker
					v-model="orderQuery.endCreateTime"
					type="datetime"
					value-format="YYYY-MM-DD HH:mm:ss"
					placeholder="结束时间"
					style="width: 180px;"
				/>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
				<el-button @click="resetForm" round>重置</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="list" style="width: 100%" stripe border ref="multipleTableRef"
				  @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center"/>
			<el-table-column fixed="left" sortable prop="id" label="ID" width="90"  align="center"/>
			<el-table-column prop="userId" label="用户ID" width="90" align="center"/>
			<el-table-column prop="userName" label="用户名" width="130" align="center"/>
			<el-table-column prop="orderNo" label="订单编号" min-width="220" align="center"/>
			<el-table-column prop="totalAmount" label="订单金额" min-width="160" align="center"/>
			<el-table-column prop="paymentType" label="支付方式" min-width="110" align="center"/>
			<el-table-column prop="orderStatus" label="商品状态" align="center" width="170">
				<template #default="scope">
					<div v-if="scope.row.orderStatus == 4">确认收货，交易成功</div>
					<div v-if="scope.row.orderStatus == 5">订单超时，交易取消</div>
					<div v-if="scope.row.orderStatus == 6">已评价，交易完成</div>
					<div v-if="scope.row.orderStatus == 1">未付款</div>
					<div v-if="scope.row.orderStatus == 2">已付款，未发货</div>
					<div v-if="scope.row.orderStatus == 3">已发货，未确认</div>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="下单时间" align="center" width="200"/>
			<el-table-column fixed="right" label="操作" min-width="200" align="center">
				<template #default="{row}">
					<el-button v-if="row.orderStatus==2" type="success" :icon="Promotion" round @click="deliver(row.id)">发货</el-button>
					<el-button type="warning" :icon="Message" round @click="details(row.id)">详情</el-button>
					<el-button v-if="row.orderStatus==5 ||row.orderStatus==6" type="danger" :icon="Delete" round @click="deleteById(row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			v-model:current-page="orderQuery.page"
			v-model:page-size="orderQuery.limit"
			:page-sizes="[5, 10, 15, 20]"
			:size="size"
			:disabled="disabled"
			:background="background"
			layout="total, sizes, prev, pager, next, jumper"
			:total="total"
			@change="loadData"
			style="margin-top: 20px; justify-content: flex-end"
		/>
	</el-card>
	
	<!--订单详情列表弹出框-->
	<el-dialog v-model="dialogFormVisible" :title="title" width="70%" :lock-scroll="false">
		<el-table :data="orderItemList" style="width: 100%" stripe border ref="multipleTableRef">
			<el-table-column prop="productName" label="商品名称" min-width="160" align="center"/>
			<el-table-column prop="productImage" label="商品图片" min-width="110" align="center">
				<template #default="scope">
					<img :src="scope.row.productImage" style="max-height: 40px; max-width: 120px;"/>
				</template>
			</el-table-column>
			<el-table-column prop="unitPrice" label="单价" align="center" width="200"/>
			<el-table-column prop="quantity" label="数量" align="center" width="200"/>
		</el-table>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="dialogFormVisible = false">关闭</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<style scoped>
	.avatar-uploader .avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
<style>
	.avatar-uploader .el-upload {
		border: 1px dashed var(--el-border-color);
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
		transition: var(--el-transition-duration-fast);
	}
	
	.avatar-uploader .el-upload:hover {
		border-color: var(--el-color-primary);
	}
</style>